local sk__zhidi = fk.CreateSkill {

  name = "sk__zhidi",

  tags = { Skill.Compulsory, },

}



sk__zhidi:addEffect(fk.EventPhaseStart, {
  name = "sk__zhidi",
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__zhidi.name) and player.phase == Player.Start and
        player:usedSkillTimes(sk__zhidi.name, Player.HistoryGame) < 4
  end,
  on_use = function(self, event, target, player, data)
    math.randomseed(tonumber(tostring(os.time()):reverse():sub(1, 6)))
    local mark = player:getTableMark("@[sk__zhidi]")
    local choices = {}
    for i = 1, 4, 1 do
      if not table.contains(mark, "sk__zhidi" .. i) then
        table.insert(choices, "sk__zhidi" .. i)
      end
    end
    table.insert(mark, table.random(choices))
    player.room:setPlayerMark(player, "@[sk__zhidi]", mark)
  end,
})

sk__zhidi:addEffect(fk.Damage, {
  name = "#sk__zhidi_delay",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target and target == player and data.card and data.card.trueName == "slash" then
      return table.contains(player:getTableMark("@[sk__zhidi]"), "sk__zhidi1")
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__zhidi")
    room:notifySkillInvoked(player, "sk__zhidi", "offensive")
    player:drawCards(1, "sk__zhidi")
  end,
})
sk__zhidi:addEffect(fk.TargetSpecified, {
  name = "#sk__zhidi_delay",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target and target == player and data.card and data.card.trueName == "slash" then
      return table.contains(player:getTableMark("@[sk__zhidi]"), "sk__zhidi2")
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__zhidi")
    room:notifySkillInvoked(player, "sk__zhidi", "offensive")
    data.use.unoffsetableList = room.alive_players
    local to = data.to
    local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if use_event == nil then return end
    room:addPlayerMark(to, MarkEnum.MarkArmorNullified)
    use_event:addCleaner(function()
      room:removePlayerMark(to, MarkEnum.MarkArmorNullified)
    end)
  end,
})

sk__zhidi:addEffect("targetmod", {
  name = "#sk__zhidi_targetmod",
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and table.contains(player:getTableMark("@[sk__zhidi]"), "sk__zhidi3") and
        scope == Player.HistoryPhase then
      return player:usedSkillTimes("sk__zhidi", Player.HistoryGame)
    end
  end,
  bypass_distances = function(self, player, skill)
    return skill.trueName == "slash_skill" and table.contains(player:getTableMark("@[sk__zhidi]"), "sk__zhidi3")
  end,
  extra_target_func = function(self, player, skill)
    if skill.trueName == "slash_skill" and table.contains(player:getTableMark("@[sk__zhidi]"), "sk__zhidi4") then
      return player:usedSkillTimes("sk__zhidi", Player.HistoryGame)
    end
  end,
})

return sk__zhidi
